﻿using System;
using System.Collections.Generic;
using GameFramework.Battle.Core;
using Gameplay.PVE.Utils;
using RootMotion.FinalIK;
using UnityEngine;
using Random = UnityEngine.Random;

namespace Gameplay.PVE.Survivor
{
    public class OmniSlashSkill: ActiveSkill
    {
        private float interval = 0.2f;
        private int totalTimes = 10;
        private float nextShootTime;
        private float startSlashTime;
        private int slashTimes = 0;
        private Vector3 startPosition;
        private Vector3 startForward;

        private float shadowInterval = 0.1f;
        private float lastShadowTime;
        
        protected override void OnStartCast()
        {
            unit.positionLocked = true;
            base.OnStartCast();
            startPosition = unit.position;
            startForward = unit.Forward;
            slashTimes = 0;
            startSlashTime = TimeManager.time;
            nextShootTime = startSlashTime;
            castTime = totalTimes * interval;
            unit.SetShowHud(false);
        }

        protected override void UpdateCast()
        {
            if (TimeManager.time >= nextShootTime)
            {
                var target = SearchTargetHelper.GetSkillTarget(unit, this);
                if (target != null)
                {
                    var startPosition = unit.position;
                    var targetPosition = target.position;
                    var angle = Random.Range(0, 360);
                    var forward = Quaternion.AngleAxis(angle, Vector3.up) * (startPosition - targetPosition).normalized;
                    forward.y = 0;
                    var pos = targetPosition + forward * -1.5f;
                    var fwd = targetPosition - pos;
                    unit.SetRenderPosition(pos,fwd);
                    bulletTargets[0].searchTargetTime = 0;
                    bulletTargets[0].unit = target;
                    CastBullet(config.bullets[0].y,config.bullets[0].z,0);
                    var effectId = config.effects[0].y;
                    var targetForward = fwd;
                    BattleEffectManager.Instance.CreateEffectAutoDispose(effectId,pos,targetForward,1);
                }
                else
                {
                    castTime = 0;
                }
                slashTimes++;
                nextShootTime = startSlashTime + slashTimes * interval;
                var random = Random.Range(2, 5);
                unit.SetAnimatorSpeed(1.6f);
                unit.Play("Skill"+random,true);
            }

            if (TimeManager.time - lastShadowTime >= shadowInterval)
            {
                unit.ShowPhantom();
                lastShadowTime = TimeManager.time;
            }
        }

        protected override void OnEnd()
        {
            unit.positionLocked = false;
            unit.SetShowHud(true);
            base.OnEnd();
            unit.SetSpeed(0);
            unit.SetAnimatorSpeed(1f);
            unit.Play("Born");
            unit.SetRenderPosition(unit.position,unit.Forward);
        }
    }
}
